

#import "LLNaviView.h"


@interface LLNaviView ()<UISearchBarDelegate>
@property (weak, nonatomic) UITextField *dd;
@property (assign, nonatomic) CGFloat leftTitleW;
@end

@implementation LLNaviView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self comfigSubViewInCommon];
    }
    return self;
}

- (void)comfigSubViewInCommon {
    // 背景
    _bgView = [UIImageView new];
    [self addSubview:_bgView];

    // 右边按钮
    _rightBarbutton = [LLButton new];
    _rightBarbutton.titleLabel.font = LLRegularFont(16);
    _rightBarbutton.titleLabel.textAlignment = NSTextAlignmentRight;
    [_rightBarbutton setTitleColor:LLColorRGB(50) forState:UIControlStateNormal];
    [_rightBarbutton addTarget:self action:@selector(rightBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightBarbutton];
}

- (void)setType:(LLNaviViewType)type {
    _type = type;
    if (type == LLNaviViewTypeNormal) {
        [self configSubViewsInNormal];
    } else if (type == LLNaviViewTypeTitleButton) {
        [self initTitleButton];
    } else if (type == LLNaviViewTypeHaveLeftButton || type == LLNaviViewTypeSearchWithBackBtn) {
        [self configSubViewsInLeftButton];
    }
}


- (void)setHaveBadgeInRightBar:(BOOL)haveBadgeInRightBar {
    _haveBadgeInRightBar = haveBadgeInRightBar;
    if (haveBadgeInRightBar) {
        _badgeBtn = [LLButton new];
        [_badgeBtn setTitleColor:[UIColor whiteColor] forState:0];
        [_badgeBtn setBackgroundImage:[UIImage imageNamed:@"ball"] forState:0];
        _badgeBtn.userInteractionEnabled = NO;
        [self addSubview:_badgeBtn];
    }
}

- (void)setBadgeNum:(NSString *)badgeNum {
    _badgeNum = badgeNum;
    if (badgeNum.length && badgeNum.integerValue) {
        CGFloat badgebtnFont = 12;
        if (badgeNum.integerValue > 9) badgebtnFont = 11;
        if (badgeNum.integerValue > 99) badgebtnFont = 8.5;
        _badgeBtn.titleLabel.font = LLRegularFont(badgebtnFont);
        [_badgeBtn setTitle:badgeNum forState:0];
        _badgeBtn.hidden = NO;
    } else {
        _badgeBtn.hidden = YES;
    }
}

- (void)configSubViewsInNormal {
    // 返回按钮
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backBtn];
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = LLRegularFont(17);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = LLColorRGB(50);
    [self addSubview:_titleLabel];
}

- (void)initTitleButton {
    _leftButton = [LLButton new];
    //    _leftButton.layer.borderWidth = 1.0;
    //    _leftButton.layer.borderColor = [UIColor whiteColor].CGColor;
    [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(leftBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_leftButton];
    
    _navTitleButton = [UIButton new];
    _navTitleButton.size = CGSizeMake(LLScreenWidth - 2 * 60, 30);
    [_navTitleButton setTitleColor:LLColorRGB(255) forState:0];
    _navTitleButton.titleLabel.font = LLRegularFont(15);
    [self addSubview:_navTitleButton];
}

- (void)setBtnImage:(NSString *)btnImage {
    _btnImage = btnImage;
    [_navTitleButton setImage:[UIImage imageNamed:btnImage] forState:0];
}

- (void)setBtnSelectedImage:(NSString *)btnSelectedImage {
    _btnSelectedImage = btnSelectedImage;
    [_navTitleButton setImage:[UIImage imageNamed:btnSelectedImage] forState:UIControlStateSelected];
}

- (void)setBtnTilte:(NSString *)btnTilte {
    _btnTilte = btnTilte;
    [_navTitleButton setTitle:btnTilte forState:0];
    [_navTitleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -_navTitleButton.imageView.frame.size.width, 0, _navTitleButton.imageView.frame.size.width + 5)];
    [_navTitleButton setImageEdgeInsets:UIEdgeInsetsMake(0, _navTitleButton.titleLabel.bounds.size.width + 5, 0, -_navTitleButton.titleLabel.bounds.size.width)];
}

- (void)configSubViewsInLeftButton {
    _leftButton = [LLButton new];
//    _leftButton.layer.borderWidth = 1.0;
//    _leftButton.layer.borderColor = [UIColor whiteColor].CGColor;
    [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(leftBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_leftButton];
    
    _textField = [UITextField new];
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.tintColor = LLMainColor;
    _textField.font = LLRegularFont(15);
    _textField.layer.cornerRadius = 15;
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5"]];
    imgView.frame = CGRectMake(10, 8, 14, 15);
    _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 34, 30)];
    [_textField.leftView addSubview: imgView];
    _textField.leftViewMode = UITextFieldViewModeAlways;
    [self addSubview:_textField];
}

- (void)setTextFieldBgColor:(UIColor *)textFieldBgColor {
    _textFieldBgColor = textFieldBgColor;
    _textField.backgroundColor = textFieldBgColor;
}

- (void)setBgImg:(NSString *)bgImg {
    _bgImg = bgImg;
    _bgView.image = [UIImage imageNamed:bgImg];
}

- (void)setHaveCover:(BOOL)haveCover {
    _haveCover = haveCover;
    if (haveCover) {
        _cover = [LLButton new];
        [_cover addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cover];
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLabel.text = title;
}

- (void)setRightBarbtnNormalTitle:(NSString *)rightBarbtnNormalTitle {
    _rightBarbtnNormalTitle = rightBarbtnNormalTitle;
    [_rightBarbutton setTitle:rightBarbtnNormalTitle forState:UIControlStateNormal];
}

- (void)setRightBarbtnSelectedTitle:(NSString *)rightBarbtnSelectedTitle {
    _rightBarbtnSelectedTitle = rightBarbtnSelectedTitle;
    [_rightBarbutton setTitle:rightBarbtnSelectedTitle forState:UIControlStateSelected];
}

- (void)setRightImage:(NSString *)rightImage {
    _rightImage = rightImage;
    [_rightBarbutton setImage:[UIImage imageNamed:rightImage] forState:UIControlStateNormal];
}

- (void)setLeftImg:(NSString *)leftImg {
    _leftImg = leftImg;
    [_leftButton setImage:[UIImage imageNamed:leftImg] forState:0];
}

- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    [_leftButton setTitle:leftTitle forState:0];
    self.leftTitleW = [CommonTools sizeOfText:leftTitle fontSize:15].width;
    if (_leftButton.currentImage) {
        _leftButton.titleLabel.font = LLRegularFont(15);
    }
    [self setNeedsLayout];
}

- (void)backBtnClick {
    if (self.didClickBackBtnBlock) {
        self.didClickBackBtnBlock();
    }
}

- (void)rightBarBtnClick:(LLButton *)btn {
    btn.selected = !btn.isSelected;
    if (self.didClickRightBarBtnBlock) {
        self.didClickRightBarBtnBlock(btn.isSelected);
    }
}

- (void)leftBarBtnClick:(LLButton *)btn {
    if (self.didClickLeftButtonBlock) {
        self.didClickLeftButtonBlock();
    }
}

- (void)coverBtnClick {
    if (self.didClickCoverBlock) {
        self.didClickCoverBlock();
    }
}


- (void)layoutSubviews {
    [super layoutSubviews];
    _bgView.frame = self.bounds;
    if (self.type == LLNaviViewTypeNormal) {
        _backBtn.frame = CGRectMake(10, self.height - 44, 30, 44);
        CGFloat titleLabelX = CGRectGetMaxX(_backBtn.frame) + 20;
        _titleLabel.frame = CGRectMake(titleLabelX, self.height - 44, self.width - 2 * titleLabelX, 44);
    } else if (self.type == LLNaviViewTypeTitleButton) {
        _leftButton.frame = CGRectMake(0, self.height - 44, 44, 44);
        _navTitleButton.frame = CGRectMake(60, self.height - 44, self.width - 120, 44);
    } else if (self.type == LLNaviViewTypeHaveLeftButton) {
        CGFloat leftBtnW = 0;
        if (self.leftTitleW) {
            leftBtnW = self.leftTitleW + 44 > LLScreenWidth * 0.35 ? LLScreenWidth * 0.35 : self.leftTitleW + 44;
        } else {
            leftBtnW = 30;
        }
        _leftButton.frame = CGRectMake(10, self.height - 44, leftBtnW, 44);
        _textField.frame = CGRectMake(_leftButton.right + 10, self.height - 44 + 7, self.width - _leftButton.right - 52 - 10, 30);
        if (_leftButton.currentImage) {
            [_leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -_leftButton.imageView.frame.size.width, 0, _leftButton.imageView.frame.size.width + 5)];
            [_leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, _leftButton.titleLabel.bounds.size.width + 5, 0, -_leftButton.titleLabel.bounds.size.width)];
        }
        _cover.frame = _textField.frame;
    } else if (self.type == LLNaviViewTypeSearchWithBackBtn) {
        _leftButton.frame = CGRectMake(10, self.height - 44, 25, 44);
        _textField.frame = CGRectMake(_leftButton.right + 13, self.height - 44 + 7, self.width - _leftButton.right - 73, 30);
    }
    _rightBarbutton.frame = CGRectMake(self.width - 52, self.height - 44, 52, 44);
    if (self.haveBadgeInRightBar) _badgeBtn.frame = CGRectMake(self.width - 22 - 3, self.height - 44, 20, 20);
}





@end
